package cn.gdust.dao;

import cn.gdust.entity.Banner;
import cn.gdust.utils.DB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BannerDao {
    public List<Banner> findAll() throws SQLException {
        List<Banner> list = new ArrayList<>();
        String sql = "SELECT * FROM  banner";
        try (Connection conn = DB.open(); PreparedStatement stmt = conn.prepareStatement(sql)) {
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Banner banner = new Banner();
                banner.setId(rs.getInt("id"));
                banner.setImage_url(rs.getString("image_url"));
                banner.setTitle(rs.getString("title"));
                banner.setDescription(rs.getString("description"));
                banner.setLink_url(rs.getString("link_url"));
                banner.setSort_order(rs.getInt("sort_order"));
                banner.setIs_enabled(rs.getInt("is_enabled"));
                banner.setCreated_at(rs.getString("created_at"));
                banner.setUpdated_at(rs.getString("updated_at"));
                list.add(banner);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
